//
//  HelpCenterTableViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/7.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "HelpCenterTableViewController.h"
#import "LPTBaseRequest.h"
#import "LPTWebViewController.h"

@interface HelpCenterTableViewController ()

@property (nonatomic, strong) NSString *apiName;
@property (nonatomic, strong) NSMutableArray *mutableArr;
@property (nonatomic, assign) NSInteger currPage;


@end

@implementation HelpCenterTableViewController

+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    HelpCenterTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HelpCenterTableViewController"];
    return vc;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [UIView new];

    
    self.apiName = @"/common/article_list";
    
    _mutableArr = [[NSMutableArray alloc] init];
    
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currPage = 1;
        [self requestList];
    }];
    
    self.tableView.mj_header = header;
//
//    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        self.currPage ++;
//        [self requestList];
//    }];
//    self.tableView.mj_footer = footer;
    
    [self.tableView.mj_header beginRefreshing];
//    _currPage = 1;
//    [self requestList];

    
}

- (void)requestList
{
    NSDictionary *body = @{};
    LPTBaseRequest  *requset = [[LPTBaseRequest alloc] initWithApi:self.apiName withBody:body withMethod:YTKRequestMethodGET];
    
    
    [requset startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        
        NSArray *model = (NSArray *)data[@"articles"];
        [self.mutableArr removeAllObjects];
        [self.mutableArr addObjectsFromArray:model];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
    
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.mutableArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HelpCenterTableCell" forIndexPath:indexPath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *dict = self.mutableArr[indexPath.row];
    cell.textLabel.text = dict[@"title"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.mutableArr[indexPath.row];

    NSString *url = [NSString stringWithFormat:@"%@/common/article_detail?id=%@", BASE_URL, dict[@"id"]];
    // 去网页
    LPTWebViewController *vc = [LPTWebViewController instanceWithUrl:url];
    [self.navigationController pushViewController:vc animated:YES];

    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
